﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using System.Data;
using System.Text.RegularExpressions;
using Geo;

namespace BLL
{
    public class DaGiacBLL
    {
        LandEntities data = new LandEntities();


        public DaGiacBLL()
        { 
        }
     

        //public void TaoDaGiac(string maDG, string toaDo, string mieuTa)
        //{
        //    OracleConnection conn;
        //    OracleCommand cmd;

        //    conn = new OracleConnection();
        //    string conString = "User id=DatDai;password=oracle;data source=LVTN";
        //    conn.ConnectionString = conString;
        //    conn.Open();
        //    string sdo_geometry = "SDO_GEOMETRY(2003,NULL,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(";

        //    string insertGeo = "insert into DaGiac values('"+ maDG+"'," +sdo_geometry +toaDo+ ")),'"+mieuTa+"')";
        //    cmd = new OracleCommand(insertGeo, conn);
        //    cmd.CommandType = CommandType.Text;
        //    cmd.ExecuteNonQuery();
        //    conn.Close();
        //}

        public void TaoDaGiac(string maDG, decimal[] toaDo, string mieuTa)
        {
            SdoGeometry polygon = new SdoGeometry();
            polygon.Sdo_Gtype = 2003;
            polygon.Sdo_Srid = null;
            polygon.Point = null;
            polygon.ElemArray = new decimal[] { 1, 1003, 1 };
            polygon.OrdinatesArray = toaDo;

            OracleConnection conn = new OracleConnection();
            conn.ConnectionString = "User id= DatDai;password =oracle;data source = LVTN";
            conn.Open();
            OracleCommand cmd = new OracleCommand("INSERT_DAGIAC", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            OracleParameter madagiac = new OracleParameter();
            madagiac.OracleDbType = OracleDbType.Char;
            madagiac.Value = maDG;

            OracleParameter hinhdang = new OracleParameter();
            hinhdang.OracleDbType = OracleDbType.Object;
            hinhdang.UdtTypeName = "SDO_GEOMETRY";
            hinhdang.Value = polygon;

            OracleParameter mieuta = new OracleParameter();
            mieuta.OracleDbType = OracleDbType.NVarchar2;
            mieuta.Value = mieuTa;

            cmd.Parameters.Add(madagiac);
            cmd.Parameters.Add(hinhdang);
            cmd.Parameters.Add(mieuta);

            cmd.ExecuteNonQuery();
            conn.Close();
        }

        #region các phương thức insert tọa độ
        
        private decimal[] ConvertToDecimalArray(string str)
        {
            string[] stArray = str.Split(',');
            decimal[] decimalArray = new decimal[stArray.Length];
            //decimal[] dArray = Array.ConvertAll<string, double>strArray,Convert.ToDouble);
            for (int i = 0; i < stArray.Length; i++)
            {
                decimalArray[i] = Convert.ToDecimal(stArray[i]);
            }
            return decimalArray;
        }
        #endregion

        public bool KiemTraHaiThuaDatGiaoNhau(string dg1, string dg2)
        {
            System.Data.Objects.ObjectParameter kiemtra = new System.Data.Objects.ObjectParameter("GIAONHAU",typeof(string));
            data.KIEMTRAHAIDAGIACGIAONHAU(dg1,dg2,kiemtra);
            if (kiemtra.Value.ToString() == "TOUCH")
                return true;
            else
                return false;
        }

        public bool KiemTraMotThuaDatChuaMotThuaDat(string dg1, string dg2)
        {
            System.Data.Objects.ObjectParameter kiemtra = new System.Data.Objects.ObjectParameter("BAOHAM", typeof(string));
            data.KIEMTRAMOTDAGIACCHUAMOTDAGIAC(dg1, dg2, kiemtra);
            if (kiemtra.Value.ToString() == "COVERS")
                return true;
            else
                return false;
        }

        public void DeleteDaGiac(string maDaGiac)
        {
            data.XOAMOTDAGIAC(maDaGiac);
        }

        public string LayMaDaGiacTuMaThuaDat(string maThuaDat)
        {
            var thuadat = data.THUADATs.SingleOrDefault(td => td.MATHUADAT.Trim() == maThuaDat);
            return thuadat.MADAGIAC.Trim();
        }

        public bool KiemTraTinhHopLeDaGiac(string maDG)
        {
            System.Data.Objects.ObjectParameter kiemtra = new System.Data.Objects.ObjectParameter("HOPLE", typeof(string));
            data.KIEMTRADAGIACHOPLE(maDG, kiemtra);
            if (kiemtra.Value.ToString() == "TRUE")
                return true;
            else
                return false;
        }
        
    }
}

